/*
 * Copyright (C) 2003, Velocityme Partnership
 * ContactDetailTypeBean.java
 *
 * Created on 09 September 2003, 07:43
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="ContactDetailType"
 *           jndi-name="ejb/ContactDetailType"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object
 *
 * @ejb.persistence table-name="contactdetailtype"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findAll()"
 *              query="SELECT OBJECT(o) FROM ContactDetailType o"
 *
 * @ejb.finder signature="com.velocityme.interfaces.ContactDetailType findByName(java.lang.String name)"
 *              query="SELECT OBJECT(o) FROM ContactDetailType o WHERE o.name = ?1"
 *
 * @ejb.finder signature="java.util.Collection findInDirectory()"
 *              query="SELECT OBJECT(o) FROM ContactDetailType o WHERE o.showInDirectory = true"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class ContactDetailTypeBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
      
    /** @ejb.create-method */
    public ContactDetailTypePK ejbCreate(com.velocityme.valueobjects.ContactDetailTypeValue p_value) throws CreateException {
        try {
            setContactDetailTypeId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.valueobjects.ContactDetailTypeValue p_value) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        this.setContactDetailTypeValue(p_value);
    }
    
    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("contactdetailtype","contactDetailTypeId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public ContactDetailTypeLocal getLocal() {
        return (ContactDetailTypeLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.ContactDetailTypeValue getContactDetailTypeValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setContactDetailTypeValue(com.velocityme.valueobjects.ContactDetailTypeValue p_value);
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="contactDetailTypeId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getContactDetailTypeId();
    public abstract void setContactDetailTypeId(java.lang.Integer contactDetailTypeId);
    
    /**
     * @ejb.persistence column-name="name"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getName();
    public abstract void setName(java.lang.String name);
    
    /**
     * @ejb.persistence column-name="showType"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Boolean getShowType();
    public abstract void setShowType(java.lang.Boolean showType);
    
    /**
     * @ejb.persistence column-name="showInDirectory"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Boolean getShowInDirectory();
    public abstract void setShowInDirectory(java.lang.Boolean showInDirectory);

    /**
     * @ejb.persistence column-name="sequenceNumber"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Integer getSequenceNumber();
    public abstract void setSequenceNumber(java.lang.Integer sequenceNumber);
    
    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="ContactDetail-ContactDetailType"
     *               role-name="ContactDetailType-has-many-ContactDetails"
     */
    public abstract java.util.Collection getContactDetailsLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setContactDetailsLocal(java.util.Collection roles);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="ContactDetailType-NotificationMechanism"
     *               role-name="ContactDetailType-has-a-NotificationMechanism"
     *               target-ejb="NotificationMechanism"
     *               target-role-name="NotificationMechanism-may-have-many-ContactDetailTypes"
     *               target-multiple="yes"
     * @jboss.relation fk-column="notificationMechanismIdFk"
     *                 related-pk-field="notificationMechanismId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.NotificationMechanismValue"
     *                   aggregate-name="NotificationMechanismValue"
     */
    public abstract com.velocityme.interfaces.NotificationMechanismLocal getNotificationMechanismLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setNotificationMechanismLocal(com.velocityme.interfaces.NotificationMechanismLocal mechanismLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="ContactDetailType-ContactDetailType"
     *               role-name="ContactDetailType-may-have-a-dependency-ContactDetailType"
     *               target-ejb="ContactDetailType"
     *               target-role-name="ContactDetailType-may-have-many-dependent-ContactDetailTypes"
     *               target-multiple="yes"
     * @jboss.relation fk-column="dependencyIdFk"
     *                 related-pk-field="contactDetailTypeId"
     */
//    public abstract com.velocityme.interfaces.ContactDetailTypeLocal getDependencyTypeLocal();
    /** @ejb.interface-method view-type="local" */
//    public abstract void setDependencyTypeLocal(com.velocityme.interfaces.ContactDetailTypeLocal typeLocal);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
